using System.Security.Claims;
using Crossborder.Common.RedisModule;
using Crossborder.Core.Const;
using Crossborder.Core.Dto;
using Crossborder.Extensions.Filters;
using Crossborder.IService;
using Microsoft.AspNetCore.Mvc;

namespace Crossborder.Web.MinimalApi.Endpoint
{
    public static class UserEndpoint
    {
        public static void UseUserEndpoint(this WebApplication app)
        {
            var group = app.MapGroup("api/User/").WithTags("User");

            group.MapGet("GetUser", async (IUserService service, HttpContext context) =>
            {
                var userId = context.Items["UserId"]?.ToString();
                var result = await service.GetUser(userId);
                return result;
            }).RequireAuthorization(AuthorizationConst.USER).AddEndpointFilter<TokenFilter>();

            group.MapPost("CreateUser", async ([FromBody] UserInputDto dto, IUserService service) =>
            {
                var result = await service.CreateUser(dto);
                return result;
            });

            group.MapPost("Login", async ([FromBody] LoginInputDto dto, IUserService service) =>
            {
                var result = await service.Login(dto);
                return result;
            });

            group.MapPost("RedisTest", async (string key, string value, int ts, IRedisWorker redis) =>
            {
                await redis.SetRedisStringAsync(key, value, ts);
            });
        }
    }
}